package servlets;

import java.io.IOException;
import javaPersistence.Producto;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.FormData;

/**
 * Servlet implementation class LoadDDL
 */
@WebServlet("/LoadDDL")
public class LoadDDL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadDDL() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ddl = "";
		String resp = "";
		try {
			HttpSession sess = request.getSession();
			FormData fdata = new FormData();
			ddl = request.getParameter("sentence");
			Producto prod = new Producto();
			if(!ddl.equals("") && ddl != null){
				resp = prod.executeDDL(ddl); 
				if(!resp.equals("true")){
					fdata.setError("Error: "+resp);
				}else{
					fdata.setMsgOK("OK: El comando fue ejecutado correctamente.");
				}
			}else{
				fdata.setError("Error: Debe digitar la sentencia DDL.");
			}
			sess.setAttribute("FormData", fdata);
			response.sendRedirect(request.getContextPath() + "/jsps/negocio/sqlDDL.jsp");
		} catch (Exception e) {
		}
	}

}
